package com.yao.demo;

import com.yao.pojo.Employee;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Macrydy
 * @date 2023/6/6
 *
 * stream的使用
 *     Stream流接口中定义了许多对于集合的操作方法，总的来说可以分为两大类：中间操作和终端操作：
 *     1.中间操作：会返回一个流，通过这种方式可以将多个中间操作连接起来，形成一个调用链，从而转换为另外 一个流。除非调用链后存在一个终端操作，否则中间操作对流不会进行任何结果处理。
 *     2.终端操作：会返回一个具体的结果，如boolean、list、integer等。
 */
public class Demo1 {
    public static List<Employee> list = new ArrayList<>();
    static {
        Employee ceo = new Employee("张三", 56, "男", 50000.42D, "浙江杭州", "浙江杭州", "ceo", null);
        Employee manager1 = new Employee("李四", 47, "女", 20000.7D, "浙江宁波", "浙江宁波", "经理", ceo);
        Employee manager2 = new Employee("王五", 45, "男", 24000.5D, "浙江金华", "浙江金华", "经理", ceo);
        Employee employee1 = new Employee("麻六", 27, "女", 7000.6D, "浙江宁波", "广东广州", "售前", manager1);
        Employee employee2 = new Employee("孙七", 28, "男", 8000.8D, "浙江宁波", "广东深圳", "售后", manager1);
        Employee employee3 = new Employee("赵八", 27, "女", 9500.2D, "浙江杭州", "云南昆明", "售前", manager2);
        Employee employee4 = new Employee("钱九", 26, "男", 9000.0D, "浙江杭州", "云南玉溪", "售后", manager2);
        list.add(ceo);
        //为了去重在加一个ceo数据
        list.add(ceo);
        list.add(manager1);
        list.add(manager2);
        list.add(employee1);
        list.add(employee2);
        list.add(employee3);
        list.add(employee4);
    }

    public static void main(String[] args) {
        //1.中间操作-filter
        //查找名字为张三的员工信息，并打印出来
        list.stream().filter(emp -> "男".equals(emp.getSex())).forEach(employee -> System.out.println(employee));

        //2.中间操作-map
        //收集公司里所有员工的名字，并打印出来
        list.stream().map(employee -> employee.getName()).forEach(name -> System.out.println(name));

        //3.中间操作-limit  注意：limit索引起始于1，如果传入负值会抛出异常。
        //获取第一个员工的数据并打印出来
        list.stream().limit(1).forEach(employee -> System.out.println(employee.getName()));

        //4.中间操作-skip
        //跳过前6条数据并打印名字出来
        list.stream().skip(6).forEach(employee -> System.out.println(employee.getName()));

        //5.中间操作-distinct
        list.stream().distinct().forEach(employee -> System.out.println(employee.getName()));


    }

}
